home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / decwrl / gethost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-25  |  2.2 KB  |  108 lines

  1. /* host - print information about a host
  2.  * originally written by Paul Vixie @DEC WRL, January 1989
  3.  */
  4.  
  5. /* DECWRL Header: host.c,v 1.1 89/04/05 15:41:12 vixie Locked $ */
  6.  
  7. #ifndef lint
  8. static char RcsId[] = "$Id: gethost.c,v 8.2 1996/10/25 17:03:11 vixie Exp $";
  9. #endif
  10.  
  11. #include <sys/param.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <arpa/nameser.h>
  16.  
  17. #include <stdio.h>
  18. #include <resolv.h>
  19. #include <netdb.h>
  20. #include <syslog.h>
  21.  
  22. #ifndef LOG_PERROR
  23. #define LOG_PERROR 0
  24. #endif
  25.  
  26. main(argc, argv)
  27.     int argc;
  28.     char **argv;
  29. {
  30.     u_char b_addr[IN6ADDRSZ];
  31.     struct hostent *host;
  32.     char **ap, **cp, *arg;
  33.     const char *prog = "amnesia";
  34.     int af = AF_INET;
  35.     int size = INADDRSZ;
  36.     int force = 0;
  37.  
  38.     if (argc < 1) {
  39.  usage:
  40.         printf("usage:  %s [-d] [-6] [-f] (hostname|ipaddr)\n", prog);
  41.         exit(1);
  42.     }
  43.     prog = *argv++; argc--;
  44. #ifdef LOG_USER
  45.     openlog(prog, LOG_PERROR, LOG_USER);
  46. #else
  47.     openlog(prog, LOG_PERROR);
  48. #endif
  49.     res_init();
  50.  
  51.     if (argc >= 1 && !strcmp(*argv, "-d")) {
  52.         _res.options |= RES_DEBUG;
  53.         argv++, argc--;
  54.     }
  55.     if (argc >= 1 && !strcmp(*argv, "-6")) {
  56.         af = AF_INET6, size = IN6ADDRSZ;
  57.         _res.options |= RES_USE_INET6;
  58.         argv++, argc--;
  59.     }
  60.     if (argc >= 1 && !strcmp(*argv, "-f")) {
  61.         force++;
  62.         argv++, argc--;
  63.     }
  64.  
  65.     if (argc < 1)
  66.         goto usage;
  67.     arg = *argv++; argc--;
  68.  
  69.     if (inet_pton(af, arg, b_addr)) {
  70.         char p[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
  71.  
  72.         printf("[%s]\n", inet_ntop(af, b_addr, p, sizeof p));
  73.         if (!(host = gethostbyaddr((char*)b_addr, size, af))) {
  74.             herror("gethostbyaddr");
  75.             exit(1);
  76.         }
  77.     } else {
  78.         printf("{%s}\n", arg);
  79.         if (force)
  80.             host = gethostbyname2(arg, af);
  81.         else
  82.             host = gethostbyname(arg);
  83.         if (!host) {
  84.             herror("gethostbyname*");
  85.             exit(1);
  86.         }
  87.     }
  88.     printf("name: %s\n", host->h_name);
  89.     if (host->h_aliases && *host->h_aliases) {
  90.         printf("aliases:");
  91.         for (cp = (char **) host->h_aliases; *cp; cp++)
  92.             printf(" %s", *cp);
  93.         printf("\n");
  94.     }
  95.     if (host->h_addr_list && *host->h_addr_list) {
  96.         printf("addresses:");
  97.         for (ap = host->h_addr_list; *ap; ap++) {
  98.             char p[sizeof
  99.                    "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"
  100.                   ];
  101.  
  102.             printf(" %s", inet_ntop(host->h_addrtype,
  103.                         *ap, p, sizeof p));
  104.         }
  105.         printf("\n");
  106.     }
  107. }
  108.